package com.longjianghuasheng.logistics.business.mine.fragment

import android.os.Bundle
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseFragment
import com.longjianghuasheng.logistics.business.mine.viewmodel.MineViewModel
import com.longjianghuasheng.logistics.databinding.FragmentMineBinding
import com.longjianghuasheng.logistics.ext.*
import com.longjianghuasheng.logistics.util.CacheUtil
import com.zackratos.ultimatebarx.ultimatebarx.addStatusBarTopPadding

class MineFragment : BaseFragment<MineViewModel, FragmentMineBinding>() {
    override fun layoutId(): Int = R.layout.fragment_mine

    override fun initView(savedInstanceState: Bundle?) {

        mDatabind.run {
            space.addStatusBarTopPadding()

            stvAbout.onClickIsLogin {
                navigate(R.id.aboutFragment, Bundle().apply {
                    putInt("id", 8)
                })
            }

            stvFeedback.onClickIsLogin { navigate(R.id.feedbackFragment) }

            stvProblem.onClickIsLogin { navigate(R.id.commonProblemFragment) }

            stvPrivacy.onClickIsLogin {
                navigate(R.id.privacyFragment)
            }

            stvSettings.onClickIsLogin { navigate(R.id.systemSettingsFragment) }

            tvMotorcadeRecruit.onClickIsLogin { navigate(R.id.fleetRecruitmentFragment) }

            tvDriverRecruit.onClickIsLogin { navigate(R.id.driverRecruitmentFragment) }

            tvBillingPeriod.onClickIsLogin {

                navigate(R.id.accountOpeningFragment, Bundle().apply {
                    putBoolean("isAccount", mViewModel.is_account == 1)
                })
            }

            tvWallet.onClickIsLogin {
                navigate(
                    if (mViewModel.is_account == 1)
                        R.id.walletFragment
                    else
                        R.id.accountOpeningFragment
                )
            }

            stvReleaseRecord.onClickIsLogin { navigate(R.id.releaseRecordFragment) }

            tvOrder.onClickIsLogin { navigate(R.id.orderFragment) }

            tvAddress.onClickIsLogin { navigate(R.id.addressListFragment) }

            tvPersonalInformation.onClickIsLogin {

                if (!mViewModel.isJump) return@onClickIsLogin
                navigate(R.id.userInfoFragment)
            }

            tvCustomerService.setOnClickListener {
                mViewModel.hot_line()
            }

            ivPhoto.onClickIsLogin {


                selectPicture {


                    mViewModel.avatar = it.realPath

                    mViewModel.avatar()
                }


            }

            if (!CacheUtil.isLogin) {
                tvName.text = "未登录"
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.result.observer(this) {
            if (!it.avatar.isNullOrBlank()) {
                mDatabind.ivPhoto.loadUrl(it.avatar)
            }

            mDatabind.tvName.text = it.real_name
            mViewModel.is_account = it.is_account ?: 0

            mDatabind.tvPersonalInformation.text = when {
                it.is_company_work == 0 && it.is_people_work == 0 -> {
                    mViewModel.isJump = true
                    "完善实名认证信息"
                }
                it.is_company_work == 2 -> "企业"
                it.is_people_work == 2 -> "个人"
                it.is_company_work == 1 || it.is_people_work == 1 -> "认证中"
                else -> " "
            }
        }
        mViewModel.resultHot_line.observe(this) {
            openPhone(it)
        }


    }

    override fun initData() {
        super.initData()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
    }

    override fun onResume() {
        super.onResume()
        mViewModel.my_info()
    }

}